﻿using Microsoft.AspNetCore.Mvc;
using TeachlyQBank.DbServer;
using TeachlyQBank.Models;

namespace TeachlyQBank.Controllers
{
    public class QuestionsController : Controller
    {
        private readonly QuestionServer _questionServer;
        private readonly QuestionTypeServer _questionTypeServer;

        public QuestionsController(QuestionServer questionServer, QuestionTypeServer questionTypeServer)
        {
            _questionServer = questionServer;
            _questionTypeServer = questionTypeServer;
        }

        // 功能:导航到添加一个试题的视图
        // 参数:不需要传递任何参数
        // 返回:对应的视图
        public IActionResult AddJumpQuestion() 
        {
            List<Knowledge> knowledges = new List<Knowledge>();
            List<QuestionType> questionTypes = new List<QuestionType>();

            knowledges = _questionServer.SltKLGServer();
            questionTypes =_questionTypeServer.GetAllQuestionTypes();

            ViewModel_AddQuestion data = new ViewModel_AddQuestion();
            data.questionType = questionTypes;
            data.knowledges = knowledges;

            return View("AddQuestion",data);
        }

        // 功能:导航到添加一个问题类型的视图
        // 参数:不需要传递任何参数
        // 返回:对应的视图
        public IActionResult AddQuestionType() 
        {
            return View();
        }

        // 功能:导航到展示所有问题类型的视图的视图
        // 参数:不需要传递任何参数
        // 返回:对应的视图
        public IActionResult ShowQuestionType()
        {
            List<QuestionType> allQuestionTypes = _questionTypeServer.GetAllQuestionTypes();
            return View(allQuestionTypes);
        }

        // 功能:添加课程界面的添加按钮的提交
        // 参数:传递一个Course类型的数据,含有添加用的课程信息
        // 返回：返回空的当前页面,注册成功还是失败都会给出对应的提示
        public IActionResult AddSumbitQuestion(Question question)
        {
            if (_questionServer.AddQuestionServer(question) > 0)
            {
                TempData["Message"] = "创建成功了";
                return RedirectToAction("ShowQuestion");
            }
            else
            {
                TempData["Message"] = "哎呀出现了意料之外的结果呢,添加失败";
            }
            return RedirectToAction("ShowQuestion");
        }
        //功能：查询出数据库中所有试题信息
        //参数：null
        //返回：返回用户信息界面
        public IActionResult ShowQuestion()
        {
            List<Question> resultList = new List<Question>();
            List<QuestionType> questionTypes = new List<QuestionType>();

            questionTypes = _questionTypeServer.GetAllQuestionTypes();
            resultList = _questionServer.SltServer();

            // 嵌套遍历来填充问题类型的名称
            foreach (Question question in resultList)
            {
                foreach( QuestionType questionType in questionTypes) 
                {
                    if (question.question_type == questionType.qt_id)
                    {
                        question.question_type = questionType.type_name;
                        break;
                    }
                }
            }

            //返回用户信息的视图
            return View("ShowQuestion", resultList);
        }
        //功能：课程信息界面删除按钮的提交
        //参数：传递一个Course类型的数据，含有数据库影响的条数
        //返回：无论删除是否成功，返回课程信息界面
        public IActionResult DelSumbit(Question question)
        {
            if (_questionServer.DelQusetionServer(question) > 0)
            {
                TempData["message"] = "删除成功";
                return RedirectToAction("ShowQuestion");
            }
            //返回用户信息的视图
            return RedirectToAction("ShowQuestion");
        }
        // 功能:修改课程信息界面的提交按钮
        // 参数:传递一个Course类型的数据,含有修改用的用户信息
        // 返回：返回课程信息界面,修改成功还是失败都会给出对应的提示
        public IActionResult UpdSumbit(Question question)
        {
            if (_questionServer.UpdServer(question) > 0)
            {
                TempData["Message"] = "修改成功了！";
                TempData["AutoFillAccount"] =   question.question_content;
            }
            else
            {
                TempData["Message"] = "哎呀出现了意料之外的结果呢,修改失败";
            }
            return RedirectToAction("ShowQuestion");
        }
        // 功能:课程信息界面的修改按钮的跳转
        // 参数:传递一个Course类型的数据,含有修改用的用户信息
        // 返回：返回课程信息修改界面
        public IActionResult UpdJump(Question question)
        {
            Question question1 = new Question();
            Question question2 = new Question();
            question1 = _questionServer.SltUpdServer(question);
            question2 = _questionServer.Slt4Server(question1);
            return View("EditQuestion", question2);
        }
        // 功能:单个课程信息界面的查询的跳转
        // 参数:传递一个Course类型的数据,含有查询的用户信息
        // 返回：返回单个课程界面
        public IActionResult SltJump(Question question)
        {
            List<Question> resultList = new List<Question>();

            resultList = _questionServer.Slt2Server(question);

            //返回用户信息的视图
            return View("ShowQuestion", resultList);
        }
        public IActionResult Showone(Question question)
        {
            Question question1 = new Question(); Question question2 = new Question();
            question1 = _questionServer.SltUpdServer(question);
            question2 = _questionServer.Slt4Server(question1);
            return View("Showone", question2);
        }
        public IActionResult Slt3Jump(Question question)
        {
            List<Question> resultList = new List<Question>();

            resultList = _questionServer.Slt3Server(question);

            //返回用户信息的视图
            return View("ShowQuestion", resultList);
        }

        [HttpPost]
        public IActionResult QTSumbit(QuestionType model)
        {
                model.qt_id=Guid.NewGuid().ToString();
                int result = _questionTypeServer.Insert(model);
                if (result > 0)
                {
                    return RedirectToAction("ShowQuestionType");                 
                }
                return View("AddQuestionType", model); // 如果模型验证失败或插入失败，返回原始表单
        }
    }
}
